from django.shortcuts import render
from datetime import datetime, timedelta
from django import http
from django.forms import models
from django.http import HttpResponseRedirect, request
from django.shortcuts import get_object_or_404, redirect, render
from django.http import HttpResponse
from django.views.generic import View
from inpatient import models
from django.utils import timezone
import hosSystem.models as models1
# Create your views here.

#----------住院部------------
class signchoose(View):
    def get(self,request,*args,**kwargs):
        return render(request,'医生或护士登录选择.html')
    def post(self,request,*args,**kwargs):
        #request.session['msg']=context  #将病人的信息传入session以在下一个视图中操作 此处为NONE bug
        if request.POST.get('id1') == '医生登陆':
            return redirect('docsignin')
        if request.POST.get('id2') == '护士登陆':
            return redirect('nursignin') 

class doclogin(View):
    """
    @description  :医生的登录页面
    """
    def get(self,request,*args,**kwargs):
        return render(request,'医生登录页面.html')
    
    def post(self,request,*args,**kwargs):
        p_id=request.POST.get('id')
        p_pwd=request.POST.get('pwd')
        try:
            doctor=get_object_or_404(models1.Doctors,id_doctor=p_id)
            if doctor.id_doctor==p_id and doctor.pwd_doctor==p_pwd:
                p_workplace=doctor.workplace_doctor
                p_type = doctor.type_doctor
                p_name=doctor.name_doctor
                context = {'id': p_id, 'name': p_name,'type':p_type,'workplace':p_workplace}
                request.session['msg1'] = context
                return redirect('/inpatient/inpatientchoose') 
            else:
                raise ValueError
        except Exception as er: #未能成功匹配或数据库中不存在该用户
        
            return render(request,'404.html',status=404)  

class nurlogin(View):
    """
    @description  :护士的登录页面
    """
    def get(self,request,*args,**kwargs):
        return render(request,'护士登录页面.html')
    
    def post(self,request,*args,**kwargs):
        p_id=request.POST.get('id')
        p_name=request.POST.get('name')
        try:
            doctor=get_object_or_404(models1.Doctors,id_doctor=p_id)
            if doctor.id_doctor==p_id and doctor.name_doctor==p_name:
                p_contact = doctor.type_doctor
                p_workplace=doctor.workplace_doctor
                context = {'id': p_id, 'name': p_name,'type':p_contact,'workplace':p_workplace}
                request.session['msg1'] = context
                return redirect('/inpatient/inpatientchoose1') 
            else:
                raise ValueError
        except Exception as er: #未能成功匹配或数据库中不存在该用户
            return render(request,'404.html',status=404)
      
        
class surgerytable(View):
    """
    @description  :保存手术表
    """

    def get(self,request,*args,**kwargs):
        return render(request,'手术表.html')

    def post(self,request,*args,**kwargs):
        surgeryDict={}
        for key in request.POST:
            if key=='id1' or key=='id2' or key=='id5' or key=='id7' or key=='id8':
                surgeryDict[key]=request.POST.get(key)
            elif key=='id3' or key=='id4':
                if request.POST.get(key)!='':
                    surgeryDict[key]=int(request.POST.get(key))
                else:
                    surgeryDict[key]=request.POST.get(key)
        Surtable=models.Surgery()
        Surtable.age_surgery=surgeryDict['id4']
        Surtable.contact_isurgery=surgeryDict['id8']
        Surtable.id_surgery=surgeryDict['id1']
        Surtable.name_inpatient2=surgeryDict['id2']
        Surtable.name_surgery2=surgeryDict['id7']
        Surtable.number_bed2=surgeryDict['id3']
        Surtable.sex_surgery=surgeryDict['id5']
        Surtable.save()
        return render(request,'手术表.html')

class inpatienttable(View):


    """
    @description  :保存住院表
    """

    def get(self,request,*args,**kwargs):
        return render(request,'住院表.html')
    
    def post(self,request,*args,**kwargs):
        inpatientDict={}
        for key in request.POST:
            if key=='id1' or key=='id2' or key=='id5' or key=='id7' or key=='id6':
                inpatientDict[key]=request.POST.get(key)
            elif key=='id3' or key=='id4':
                if request.POST.get(key)!='':
                    inpatientDict[key]=int(request.POST.get(key))
                else:
                    inpatientDict[key]=request.POST.get(key)
        Inptable=models.InPatient()
        Inptable.age_inpatient=inpatientDict['id4']
        Inptable.contact_inpatient=inpatientDict['id7']
        Inptable.id_inpatient=inpatientDict['id1']
        Inptable.name_inpatient1=inpatientDict['id2']
        Inptable.number_bed1=inpatientDict['id5']
        Inptable.sex_inpatient=inpatientDict['id3']
        Inptable.id_doctor=inpatientDict['id6']
        Inptable.save()
        return render(request,'住院表.html')

class inpatientchoose(View):
    def get(self,request,*args,**kwargs):
        try:
            context = request.session.get('msg1')
            request.session['msg2'] = context
            if context is None:
                raise ValueError
            else:
                return render(request, '医生页面.html', context=context)
        except ValueError as ve:
            return render(request, 'warnSign.html')
        
    def post(self,request,*args,**kwargs):
        #request.session['msg']=context  #将病人的信息传入session以在下一个视图中操作 此处为NONE bug
        if request.POST.get('id1') == '病人信息':
            context=request.session.get('msg2')
            request.session['msg2'] = context
            return redirect('inpatientchoose/inpatientmsg')
        if request.POST.get('id2') == '手术表':
            return redirect('surgery') 
        if request.POST.get('id3') == '住院表':
            return redirect('inpatient') 

class inpatientchoose1(View):
    def get(self,request,*args,**kwargs):
        try:
            context = request.session.get('msg1')
            request.session['msg2'] = context
            if context is None:
                raise ValueError
            else:
                return render(request, '护士页面.html', context=context)
        except ValueError as ve:
            return render(request, 'warnSign.html')
        
    def post(self,request,*args,**kwargs):
        #request.session['msg']=context  #将病人的信息传入session以在下一个视图中操作 此处为NONE bug
        if request.POST.get('id1') == '病人信息':
            context=request.session.get('msg2')
            request.session['msg2'] = context
            return redirect('inpatientchoose1/inpatientmsg1')
        if request.POST.get('id3') == '住院表':
            return redirect('inpatient') 
class inpatientmsg(View):
    """
    @description  :医生查看病人信息及住院和手术
    """
    def get(self,request,*args,**kwargs):
        try:
            context = request.session.get('msg2')
            request.session['msg3'] = context
            if context is None:
                raise ValueError
            else:
                p_workplace=context['workplace']
                A = models.InPatient.objects.filter(workplace_doctor=p_workplace)
                #print(A)
                '''
                name_sur=models.InPatient.objects.filter(id_doctor=p_id).name_surgery1
                name_inp=models.InPatient.objects.filter(id_doctor=p_id).name_inpatient1
                sex_inp=models.InPatient.objects.filter(id_doctor=p_id).sex_inpatient
                contact_inp=models.InPatient.objects.filter(id_doctor=p_id).contact_inpatient
                num_bed=models.InPatient.objects.filter(id_doctor=p_id).number_bed1
                context={'name_sur':name_sur,'name_inp':name_inp,'sex_inp':sex_inp,'contact_inp':contact_inp,'num_bed':num_bed} 
                '''
                context['pinfo'] = []
                
                for i in A:
                    a=[]
                    a.append(i.name_surgery1)
                    a.append(i.name_inpatient1)
                    a.append('男' if i.sex_inpatient == 0 else '女')
                    a.append(i.contact_inpatient)
                    a.append(i.number_bed1)
                    context['pinfo'].append(a)
                    '''
                context['pinfo'].append(A.name_surgery1)
                context['pinfo'].append(A.name_inpatient1)
                context['pinfo'].append('男' if A.sex_inpatient == 0 else '女')
                context['pinfo'].append(A.contact_inpatient)
                context['pinfo'].append(A.number_bed1)'''
                return render(request, '医生病人页面.html', context=context)
        except ValueError as ve:
            return render(request, 'warnSign.html')
    def post(self,request,*args,**kwargs):
        '''
        context = request.session.get('msg3')
        p_id=context['id']
        request.session['msg3'] = context
        A = models.InPatient.objects.filter(id_doctor=p_id)
        '''
        if request.POST.get('id1') == '手术表':
            return redirect('surgery') 
        if request.POST.get('id2') == '住院表':
            return redirect('inpatient') 

class inpatientmsg1(View):
    """
    @description  :护士查看病人信息及住院
    """
    def get(self,request,*args,**kwargs):
        try:
            context = request.session.get('msg2')
            request.session['msg3'] = context
            if context is None:
                raise ValueError
            else:
                p_workplace=context['workplace']
                A = models.InPatient.objects.filter(workplace_doctor=p_workplace)
                #print(A)
                context['pinfo'] = []
                
                for i in A:
                    a=[]
                    a.append(i.name_surgery1)
                    a.append(i.name_inpatient1)
                    a.append('男' if i.sex_inpatient == 0 else '女')
                    a.append(i.contact_inpatient)
                    a.append(i.number_bed1)
                    context['pinfo'].append(a)
                    '''
                context['pinfo'].append(A.name_surgery1)
                context['pinfo'].append(A.name_inpatient1)
                context['pinfo'].append('男' if A.sex_inpatient == 0 else '女')
                context['pinfo'].append(A.contact_inpatient)
                context['pinfo'].append(A.number_bed1)'''
                return render(request, '医生病人页面.html', context=context)
        except ValueError as ve:
            return render(request, 'warnSign.html')
    def post(self,request,*args,**kwargs):
        '''
        context = request.session.get('msg3')
        p_id=context['id']
        request.session['msg3'] = context
        A = models.InPatient.objects.filter(id_doctor=p_id)
        '''

        if request.POST.get('id2') == '住院表':
            return redirect('inpatient') 

